﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace AppBrowser
{
    public partial class frmAddApp : Form
    {
        private frmAppBrowser appForm;
        private AppData data;
        private DataRow AppRow;
        private DataViewManager dsView;

        private bool EditMode = false;
        private AppItem _App;
        public AppItem App
        {
            get { return _App; }
            set { _App = value; }
        }

        public frmAddApp(frmAppBrowser tform, AppData d)
        {
            InitializeComponent();
            data = d;
            appForm = tform;

        }

        private void frmAddApp_Load(object sender, EventArgs e)
        {
            // create new row
            // DataTable dt = data.GetApps();
            object[] cat = (object[])appForm.GetCurrentCategory();


            dsView = data.AppDataSet.DefaultViewManager;
            cmbCategory.DataSource = dsView;
            cmbCategory.DisplayMember = "Category.name";
            cmbCategory.ValueMember = "Category_Id";
            try
            {
                cmbCategory.SelectedValue = cat[0];
            }
            catch { }
            cmbLicense.Items.AddRange(data.BuildLicenseArray());

            // if app set before here, than its edit mode, else add
            if (_App != null)
            {
                EditMode = true;
            }
            else
            {
                // just add
                EditMode = false;
                _App = new AppItem();
            }

            FillFormValues();
        }

        private void FillFormValues()
        {
            if (EditMode)
            {
                this.Text = "Edit";
                txtAppName.Text = _App.AppName;
                txtAppUrl.Text = _App.AppUrl;
                txtPublisher.Text = _App.Publisher;
                txtPublisherUrl.Text = _App.PublisherUrl;
                txtSysReqs.Text = _App.SystemRequirements;

                cmbCategory.SelectedValue = _App.Category_Id;
                cmbLicense.Text = _App.License;


            }

            FillDataGrid();

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
           
            
            if (txtAppName.Text.Length == 0)
            {
                MessageBox.Show("Please enter a name for the application");
                return;
            }
            if (txtAppUrl.Text.Length == 0)
            {
                MessageBox.Show("How about a URL for the application");
                return;
            }

            _App.AppName = txtAppName.Text;
            _App.AppUrl = txtAppUrl.Text;
            _App.Publisher = txtPublisher.Text;
            _App.PublisherUrl = txtPublisherUrl.Text;
            _App.License = cmbLicense.Text;
            _App.Description = txtDescription.Text;

            // get category id
            _App.Category_Id = cmbCategory.SelectedValue.ToString();
           //  this.Close();
        }

        private void btnAddVersion_Click(object sender, EventArgs e)
        {
            frmAddAppVersion fa = new frmAddAppVersion();
            if (fa.ShowDialog() == DialogResult.OK)
            {
                // Add version data to this App
                _App.VersionList.Add(fa.AppVersionData);
                FillDataGrid();
                
              
            }
        }

        private void FillDataGrid()
        {
            dataGridView1.Rows.Clear();
            if (_App.VersionList.Count > 0)
            {
                dataGridView1.Rows.Add(_App.VersionList.Count);
                int cnt = -1;
                foreach (AppVersion av in _App.VersionList)
                {
                    cnt++;
                    DataGridViewRow dr = dataGridView1.Rows[cnt];
                    dr.Cells[0].Value = av.VersionNumber;
                    dr.Cells[1].Value = av.VersionType;
                    dr.Cells[2].Value = av.DownloadUrl;
                }
            }
        }

        private void AddDGRow(AppVersion appVer)
        {
            dataGridView1.Rows.Add(new string[] {appVer.VersionNumber,
                                                 appVer.VersionType, 
                                                 appVer.DownloadUrl });
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                // edit version info
                int SelectedIndex = dataGridView1.SelectedRows[0].Index;
                bool edit = false;

                frmAddAppVersion aVer = new frmAddAppVersion();
                if (SelectedIndex < App.VersionList.Count)
                {
                    edit = true;
                    // edit current
                    aVer.AppVersionData = _App.VersionList[SelectedIndex];
                }
                if (aVer.ShowDialog() == DialogResult.OK)
                {
                    if (!edit)
                    {
                        _App.VersionList.Add(aVer.AppVersionData);
                        AddDGRow(aVer.AppVersionData);
                    }
                    else
                    {
                        FillDataGrid();

                    }
                }
            }
        }

        private void AddEditVersion()
        {
            frmAddAppVersion fa = new frmAddAppVersion();
            if (fa.ShowDialog() == DialogResult.OK)
            {
                // Add version data to this App
                _App.VersionList.Add(fa.AppVersionData);
                FillDataGrid();
            }

        }
    }
}
